package com.huijiet.service.impl;


import com.huijiet.base.auth.JWTFactory;
import com.huijiet.base.common.CommonResult;
import com.huijiet.base.common.DataGrid;
import com.huijiet.base.common.Ref;
import com.huijiet.base.utils.Md5Util;
import com.huijiet.base.utils.TimeUtil;
import com.huijiet.entity.SysMember;
import com.huijiet.mapper.SysMemberMapper;
import com.huijiet.param.MemberParam;
import com.huijiet.service.IMemberService;
import com.huijiet.vo.MemberVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MemberServiceImpl implements IMemberService {

    @Resource
    private SysMemberMapper memberMapper;
    @Resource
    private JWTFactory jwtFactory;

    /**用户登录*/
    public CommonResult<MemberVo.LoginVo> login(MemberParam.LoginParam param){

        SysMember member = memberMapper.getByUsername(param.getUsername());
        if(member==null){
            return Ref.error("错误的用户名或密码");
        }
        String dbPass = Md5Util.makePwd(param.getPassword(), member.getSalt());
        if(!dbPass.equals(member.getPassword())){
            return Ref.error("错误的用户名或密码");
        }
        if(member.getUsable()==0){
            return Ref.error("抱歉，用户被禁用");
        }

        member.setLastLoginTime(new Date());
        memberMapper.updateById(member);

        String token = jwtFactory.createToken(member.getId(), 1, "web");

        MemberVo.LoginVo vo = new MemberVo.LoginVo();
        vo.setRealName(member.getRealName());
        vo.setUserId(member.getId());
        vo.setUsername(member.getUsername());
        vo.setToken(token);

        return Ref.ok(vo);
    }

    /**修改密码*/
    public CommonResult changePass(MemberParam.ChangePassParam param){
        SysMember member = memberMapper.getByUsername(param.getUsername());
        if(member==null){
            return Ref.error("错误的用户名或密码");
        }
        String salt = member.getSalt();
        String dbPass = Md5Util.makePwd(param.getOldPass(),salt);
        if(!dbPass.equals(member.getPassword())){
            return Ref.error("旧密码不正确");
        }
        dbPass = Md5Util.makePwd(param.getNewPass(),salt);
        member.setPassword(dbPass);
        memberMapper.updateById(member);
        return Ref.okMsg("密码修改成功");
    }

    /**后台获取用户列表*/
    public CommonResult list(MemberParam.ListParam param){

        List<SysMember> list = memberMapper.list(param.getStart(), param.getNum());
        int count = memberMapper.listCount();

        List<MemberVo.ListVo> vos = new ArrayList<>();
        for(SysMember member : list){
            MemberVo.ListVo vo = new MemberVo.ListVo();
            vo.setUserId(member.getId());
            vo.setUsername(member.getUsername());
            vo.setRealName(member.getRealName());
            vo.setUsable(member.getUsable());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(member.getCreateTime()));
            vo.setLastLoginTime(member.getLastLoginTime()==null?"":TimeUtil.formaterYYYYMMddHHmm(member.getLastLoginTime()));
            vos.add(vo);
        }
        DataGrid<MemberVo.ListVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    /**禁用用户*/
    public CommonResult disable(MemberParam.DisableParam param){
        SysMember member = memberMapper.selectById(param.getUserId());
        if(member==null){
            return Ref.error("错误的用户编号");
        }
        member.setUsable(0);
        memberMapper.updateById(member);
        return Ref.ok();
    }

    /**启用用户*/
    public CommonResult usable(MemberParam.UsableParam param){
        SysMember member = memberMapper.selectById(param.getUserId());
        if(member==null){
            return Ref.error("错误的用户编号");
        }
        member.setUsable(1);
        memberMapper.updateById(member);
        return Ref.ok();
    }

    /**重置密码*/
    public CommonResult resetPass(MemberParam.ResetPassParam param){
        SysMember member = memberMapper.selectById(param.getUserId());
        if(member==null){
            return Ref.error("错误的用户编号");
        }

        //将密码设置为默认密码 123456
        String dbPass = Md5Util.makePwd("123456", member.getSalt());
        member.setPassword(dbPass);
        memberMapper.updateById(member);

        return Ref.ok();
    }

    /**新增用户*/
    public CommonResult add(MemberParam.AddParam param){
        SysMember member = memberMapper.getByUsername(param.getUsername());
        if(member!=null){
            return Ref.error("用户名已经存在");
        }
        member = new SysMember();
        member.setUsername(param.getUsername());
        member.setRealName(param.getRealName());
        member.setUsable(param.getUsable());

        String salt = Md5Util.getSalt();
        String dbPass = Md5Util.makePwd(param.getPassword(), salt);
        member.setPassword(dbPass);
        member.setSalt(salt);
        member.setCreateTime(new Date());
        memberMapper.insert(member);
        return Ref.ok();
    }

    /**修改用户*/
    public CommonResult modify(MemberParam.ModifyParam param){
        SysMember member = memberMapper.selectById(param.getUserId());
        if(member==null){
            return Ref.error("错误的用户编号");
        }
        member.setRealName(param.getRealName());
        member.setUsable(param.getUsable());
        memberMapper.updateById(member);
        return Ref.ok();
    }

    /**删除用户*/
    public CommonResult delete(MemberParam.DeleteParam param){
        SysMember member = memberMapper.selectById(param.getUserId());
        if(member==null){
            return Ref.error("错误的用户编号");
        }
        if(member.getUsable()==1){
            return Ref.error("只有被禁用的用户才能删除");
        }
        memberMapper.deleteById(param.getUserId());
        return Ref.ok();
    }
}
